package com.zwx.bingo.autoconfigure.context;

import com.zwx.bingo.context.ContextHolder;
import com.zwx.bingo.context.EnvironmentHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StopWatch;

/**
 * @author zoo
 */
@Configuration
@AutoConfigureOrder
@Slf4j
@ConditionalOnClass(value = {ContextHolder.class, EnvironmentHolder.class})
public class ContextHolderAutoConfiguration {

    public ContextHolderAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean
    public ContextHolder applicationContextHolder() {
        log.debug("Starting Context#ContextHolder configuration");
        StopWatch watch = new StopWatch();
        watch.start();
        ContextHolder contextHolder = new ContextHolder();
        watch.stop();
        log.debug("Finishing Context#ContextHolder configuration in {} ms", watch.getTotalTimeMillis());
        return contextHolder;
    }

    @Bean
    @ConditionalOnMissingBean
    public EnvironmentHolder environmentHolder() {

        log.debug("Starting Context#EnvironmentHolder configuration");
        StopWatch watch = new StopWatch();
        watch.start();
        EnvironmentHolder environmentHolder = new EnvironmentHolder();
        watch.stop();
        log.debug("Finishing Context#EnvironmentHolder configuration in {} ms", watch.getTotalTimeMillis());
        return environmentHolder;
    }

}